home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / RKMLibsPrgs / iffparse / clipftxt.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  9KB  |  275 lines

  1. ;/* clipftxt.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -j73 clipftxt.c
  3. Blink FROM LIB:c.o,clipftxt.o TO clipftxt LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. */
  6.  
  7. /*
  8. Copyright (c) 1992 Commodore-Amiga, Inc.
  9.  
  10. This example is provided in electronic form by Commodore-Amiga, Inc. for
  11. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  12. published by Addison-Wesley (ISBN 0-201-56774-1).
  13.  
  14. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  15. information on the correct usage of the techniques and operating system
  16. functions presented in these examples.  The source and executable code
  17. of these examples may only be distributed in free electronic form, via
  18. bulletin board or as part of a fully non-commercial and freely
  19. redistributable diskette.  Both the source and executable code (including
  20. comments) must be included, without modification, in any copy.  This
  21. example may not be published in printed form or distributed with any
  22. commercial product.  However, the programming techniques and support
  23. routines set forth in these examples may be used in the development
  24. of original executable software products for Commodore Amiga computers.
  25.  
  26. All other rights reserved.
  27.  
  28. This example is provided "as-is" and is subject to change; no
  29. warranties are made.  All use is at your own risk. No liability or
  30. responsibility is assumed.
  31. */
  32.  
  33. /*
  34. *
  35. * clipftxt.c:   Writes ASCII text to clipboard unit as FTXT
  36. *               (All clipboard data must be IFF)
  37. *
  38. * Usage: clipftxt unitnumber
  39. *
  40. * To convert to an example of reading only, comment out #define WRITEREAD
  41. */
  42.  
  43. #include <exec/types.h>
  44. #include <exec/memory.h>
  45. #include <libraries/dos.h>
  46. #include <libraries/iffparse.h>
  47.  
  48. #include <clib/exec_protos.h>
  49. #include <clib/dos_protos.h>
  50. #include <clib/iffparse_protos.h>
  51. #include <stdlib.h>
  52. #include <stdio.h>
  53. #include <string.h>
  54.  
  55. #ifdef LATTICE
  56. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  57. int chkabort(void) { return(0); }  /* really */
  58. #endif
  59.  
  60. /* Causes example to write FTXT first, then read it back
  61.  * Comment out to create a reader only
  62.  */
  63. #define WRITEREAD
  64.  
  65.  
  66. #define MINARGS 2
  67.  
  68. /* 2.0 Version string for c:Version to find */
  69. UBYTE vers[] = "\0$VER: clipftxt 37.2";
  70.  
  71. UBYTE usage[] = "Usage: clipftxt unitnumber (use zero for primary unit)";
  72.  
  73. /*
  74.  * Text error messages for possible IFFERR_#? returns from various
  75.  * IFF routines.  To get the index into this array, take your IFFERR code,
  76.  * negate it, and subtract one.
  77.  *  idx = -error - 1;
  78.  */
  79. char    *errormsgs[] = {
  80.         "End of file (not an error).",
  81.         "End of context (not an error).",
  82.         "No lexical scope.",
  83.         "Insufficient memory.",
  84.         "Stream read error.",
  85.         "Stream write error.",
  86.         "Stream seek error.",
  87.         "File is corrupt.",
  88.         "IFF syntax error.",
  89.         "Not an IFF file.",
  90.         "Required call-back hook missing.",
  91.         "Return to client.  You should never see this."
  92. };
  93.  
  94. #define RBUFSZ 512
  95.  
  96. #define  ID_FTXT        MAKE_ID('F','T','X','T')
  97. #define  ID_CHRS        MAKE_ID('C','H','R','S')
  98.  
  99. struct Library *IFFParseBase;
  100.  
  101. UBYTE mytext[]="This FTXT written to clipboard by clipftxt example.\n";
  102.  
  103. void main(int argc, char **argv)
  104. {
  105.     struct IFFHandle    *iff = NULL;
  106.     struct ContextNode  *cn;
  107.     long                error=0, unitnumber=0, rlen;
  108.     int textlen;
  109.     UBYTE readbuf[RBUFSZ];
  110.  
  111.         /* if not enough args or '?', print usage */
  112.         if(((argc)&&(argc<MINARGS))||(argv[argc-1][0]=='?'))
  113.                 {
  114.                 printf("%s\n",usage);
  115.                 exit(RETURN_WARN);
  116.                 }
  117.  
  118.         unitnumber = atoi(argv[1]);
  119.  
  120.         if (!(IFFParseBase = OpenLibrary ("iffparse.library", 0L)))
  121.                 {
  122.                 puts("Can't open iff parsing library.");
  123.                 goto bye;
  124.                 }
  125.  
  126.         /*
  127.          * Allocate IFF_File structure.
  128.          */
  129.         if (!(iff = AllocIFF ()))
  130.                 {
  131.                 puts ("AllocIFF() failed.");
  132.                 goto bye;
  133.                 }
  134.  
  135.         /*
  136.          * Set up IFF_File for Clipboard I/O.
  137.          */
  138.         if (!(iff->iff_Stream = (ULONG) OpenClipboard (unitnumber)))
  139.                 {
  140.                 puts ("Clipboard open failed.");
  141.                 goto bye;
  142.                 }
  143.         else printf("Opened clipboard unit %ld\n",unitnumber);
  144.  
  145.         InitIFFasClip (iff);
  146.  
  147. #ifdef WRITEREAD
  148.  
  149.         /*
  150.          * Start the IFF transaction.
  151.          */
  152.         if (error = OpenIFF (iff, IFFF_WRITE))
  153.                 {
  154.                 puts ("OpenIFF for write failed.");
  155.                 goto bye;
  156.                 }
  157.  
  158.         /*
  159.          * Write our text to the clipboard as CHRS chunk in FORM FTXT
  160.          *
  161.          * First, write the FORM ID (FTXT)
  162.          */
  163.         if(!(error=PushChunk(iff, ID_FTXT, ID_FORM, IFFSIZE_UNKNOWN)))
  164.                 {
  165.                 /* Now the CHRS chunk ID followed by the chunk data
  166.                  * We'll just write one CHRS chunk.
  167.                  * You could write more chunks.
  168.                  */
  169.                 if(!(error=PushChunk(iff, 0, ID_CHRS, IFFSIZE_UNKNOWN)))
  170.                         {
  171.                         /* Now the actual data (the text) */
  172.                         textlen = strlen(mytext);
  173.                         if(WriteChunkBytes(iff, mytext, textlen) != textlen)
  174.                                 {
  175.                                 puts("Error writing CHRS data.");
  176.                                 error = IFFERR_WRITE;
  177.                                 }
  178.                         }
  179.                 if(!error) error = PopChunk(iff);
  180.                 }
  181.         if(!error) error = PopChunk(iff);
  182.  
  183.  
  184.         if(error)
  185.                 {
  186.                 printf ("IFF write failed, error %ld: %s\n",
  187.                         error, errormsgs[-error - 1]);
  188.                 goto bye;
  189.                 }
  190.         else printf("Wrote text to clipboard as FTXT\n");
  191.  
  192.         /*
  193.          * Now let's close it, then read it back
  194.          * First close the write handle, then close the clipboard
  195.          */
  196.         CloseIFF(iff);
  197.         if (iff->iff_Stream) CloseClipboard ((struct ClipboardHandle *)
  198.                                                 iff->iff_Stream);
  199.  
  200.         if (!(iff->iff_Stream = (ULONG) OpenClipboard (unitnumber)))
  201.                 {
  202.                 puts ("Reopen of Clipboard failed.");
  203.                 goto bye;
  204.                 }
  205.         else printf("Reopened clipboard unit %ld\n",unitnumber);
  206.  
  207. #endif /* WRITEREAD */
  208.  
  209.         if (error = OpenIFF (iff, IFFF_READ))
  210.                 {
  211.                 puts ("OpenIFF for read failed.");
  212.                 goto bye;
  213.                 }
  214.  
  215.         /* Tell iffparse we want to stop on FTXT CHRS chunks */
  216.         if (error = StopChunk(iff, ID_FTXT, ID_CHRS))
  217.                 {
  218.                 puts ("StopChunk failed.");
  219.                 goto bye;
  220.                 }
  221.  
  222.         /* Find all of the FTXT CHRS chunks */
  223.         while(1)
  224.                 {
  225.                 error = ParseIFF(iff,IFFPARSE_SCAN);
  226.                 if(error == IFFERR_EOC) continue;       /* enter next context */
  227.                 else if(error) break;
  228.  
  229.                 /* We only asked to stop at FTXT CHRS chunks
  230.                  * If no error we've hit a stop chunk
  231.                  * Read the CHRS chunk data
  232.                  */
  233.                 cn = CurrentChunk(iff);
  234.  
  235.                 if((cn)&&(cn->cn_Type == ID_FTXT)&&(cn->cn_ID == ID_CHRS))
  236.                         {
  237.                         printf("CHRS chunk contains:\n");
  238.                         while((rlen = ReadChunkBytes(iff,readbuf,RBUFSZ)) > 0)
  239.                                 {
  240.                                 Write(Output(),readbuf,rlen);
  241.                                 }
  242.                         if(rlen < 0)    error = rlen;
  243.                         }
  244.                 }
  245.  
  246.         if((error)&&(error != IFFERR_EOF))
  247.                 {
  248.                 printf ("IFF read failed, error %ld: %s\n",
  249.                         error, errormsgs[-error - 1]);
  250.                 }
  251.  
  252. bye:
  253.         if (iff) {
  254.                 /*
  255.                  * Terminate the IFF transaction with the stream.  Free
  256.                  * all associated structures.
  257.                  */
  258.                 CloseIFF (iff);
  259.  
  260.                 /*
  261.                  * Close the clipboard stream
  262.                  */
  263.                 if (iff->iff_Stream)
  264.                                 CloseClipboard ((struct ClipboardHandle *)
  265.                                                 iff->iff_Stream);
  266.                 /*
  267.                  * Free the IFF_File structure itself.
  268.                  */
  269.                 FreeIFF (iff);
  270.                 }
  271.         if (IFFParseBase)       CloseLibrary (IFFParseBase);
  272.  
  273.         exit (RETURN_OK);
  274. }
  275.